/**
 * @author harry
 * @date 2022/2/23/20:21
 */

package com.serviceOrchestration.controller;

import com.alibaba.fastjson.JSON;
import com.serviceOrchestration.annotation.RoleRestriction;
import com.serviceOrchestration.pojo.ReturnData;
import com.serviceOrchestration.util.Role;
import lombok.SneakyThrows;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/test")
public class TestController {

    @RoleRestriction({Role.USR, Role.ADM})
    @GetMapping("/userAndAdmin")
    @ResponseBody
    public ReturnData<?> userAndAdmin() {
        return ReturnData.ok("user & admin");
    }

    @SneakyThrows
    @RoleRestriction({Role.USR})
    @PostMapping("/user")
    public void user(HttpServletResponse response) {
        response.getWriter().write(JSON.toJSONString(ReturnData.ok("user")));
    }

    @RoleRestriction({Role.ADM})
    @PutMapping("/admin")
    @ResponseBody
    public ReturnData<?> admin() {
        return ReturnData.ok("admin");
    }

    @GetMapping("/string")
    @ResponseBody
    public String returnString() {
        return "Hello";
    }
}
